<template>
  <div class="app-container">
    <!-- 搜索条件 -->
    <div class="filter-container">
      <el-input
        @keyup.enter.native="handleFilter"
        style="width: 200px"
        class="filter-item"
        placeholder="章节名"
        v-model="keyword"
      >
      </el-input>

      <!-- <el-select clearable class="filter-item" style="width: 130px" v-model="listQuery.type" placeholder="类型">
        <el-option v-for="item in  typeOptions" :key="item.key" :label="item.display_name+'('+item.key+')'" :value="item.key">
        </el-option>
      </el-select> -->

      <el-button
        class="filter-item"
        type="primary"
        v-waves
        icon="search"
        @click="handleFilter"
        >搜索</el-button
      >
      <el-button
        class="filter-item"
        type="primary"
        @click="handleCreate"
        icon="edit"
        >添加</el-button
      >

      <el-button
        class="filter-item"
        type="primary"
        @click="handleDelAll"
        icon="edit"
        >批量删除</el-button
      >
    </div>

    <!-- 表格 v-loading.body="listLoading"-->
    <el-table
      ref="multipleTable"
      @selection-change="handleSelectionChange"
      :data="chapters"
      border
      fit
      highlight-current-row
    >
      <el-table-column type="selection" width="50"> </el-table-column>

      <el-table-column align="center" label="序号" width="70">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>

      <el-table-column label="书名" width="" align="center" prop="comic_title">
        <template slot-scope="scope">
          {{ scope.row.comic_title }}
        </template>
      </el-table-column>

      <el-table-column label="章节名" width="" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="showContent(scope.row)">{{
            scope.row.comic_chap_title
          }}</el-button>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="listQuery.currPage"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="listQuery.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 新增弹窗 -->
    <el-dialog title="新增卷" :visible.sync="addDialog" @close="closeDialog">
      <el-form
        class="small-space"
        :model="chapter"
        label-position="left"
        label-width="70px"
        style="width: 400px; margin-left: 50px"
      >
        <el-form-item label="章节名">
          <el-input v-model="chapter.comic_chap_title"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialog = false">取 消</el-button>

        <el-button type="primary" @click="handleCreateSubmit">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :visible.sync="ContentDialog"
      :title="comic_chap_title"
      @close="closeContentBox"
      center
      width="60%"
    >
      <el-upload
        class="upload-demo"
        :action="`${$http.defaults.baseURL}/update_comic_cover`" 
        :on-preview="handlePictureCardPreview"
        :on-remove="handleRemove"
        list-type="picture-card"
        :file-list="contents"
        :on-success="afterUpload"
      >
        <i class="el-icon-plus"></i>
      </el-upload>
      <el-dialog :visible.sync="dialogVisibleIMG">
        <img width="100%" :src="dialogImageUrl" alt="" />
      </el-dialog>
      <span slot="footer" class="dialog-footer">
        <el-button @click="ContentDialog = false">取 消</el-button>
        <el-button type="primary" @click="submitContent">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 弹框
      dialogVisible: false,
      dialogVisibleIMG: false,
      dialogImageUrl: "",
      chapters: [],
      chapter: { comic_chap_title: "" },
      // list: null,
      // listLoading: true,
      editDialog: false,
      addDialog: false,

      keyword: "",
      list: null, //表格list
      total: null,
      novel_id: null,
      listQuery: {
        currPage: 1,
        pageSize: 10,
      },
      is_edit: false,
      multipleSelection: [],
      comic_chap_title: "",
      contents: [],
      ContentDialog: false,
      comic_chap_id:'', //修改内容时章节id
    };
  },
  async created() {
    this.comic_id = this.$route.query.comic_id;
    console.log(this.comic_id);
    this.getList();
  },
  methods: {
    //获取列表数据
    async getList() {
      let res = await this.$http.get("/comic_chapters", {
        params: {
          comic_id: this.comic_id,
          pageSize: this.listQuery.pageSize,
          currPage: this.listQuery.currPage,
        },
      });
      this.chapters = res.data.chapters;
      this.listQuery.currPage = parseInt(res.data.currPage);
      this.listQuery.pageSize = parseInt(res.data.pageSize);
      this.total = res.data.total;
    },
    //编辑
    handleEdit(index, row) {
      this.addDialog = true;
      this.is_edit = true; //处于打开对话框状态
      this.chapter = row;
      console.log("编辑的row：", index, "-----", row);
    },
    //单个删除
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delFun(row);
          //前端删除。
          this.chapters.splice(index, 1);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    delFun(row) {
      this.$http
        .delete("/delete_comic_chapter", {
          params: { comic_chap_id: row.comic_chap_id },
        })
        .then((res) => {});
    },
    //批量删除
    handleDelAll() {
      var vm = this;
      console.log("批量删除选择的row：", vm.multipleSelection);
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定批量删除",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //前端依次执行单个删除操作
          vm.chapters = vm.chapters.filter((item) => {
            for (let selection of vm.multipleSelection) {
              if (item.comic_chap_id === selection.comic_chap_id) {
                return false;
              }
            }
            return true;
          });
          for (var index in vm.multipleSelection) {
            console.log("======", vm.multipleSelection[index]);
            vm.delFun(vm.multipleSelection[index]);
          }
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //搜索
    async handleFilter() {
      let res = await this.$http.get("/search_comic_chapter", {
        params: {
          comic_id: this.comic_id,
          keyword: this.keyword,
          pageSize: this.listQuery.pageSize,
          currPage: this.listQuery.currPage,
        },
      });
      this.chapters = res.data.chapters;
      this.listQuery.currPage = parseInt(res.data.currPage);
      this.listQuery.pageSize = parseInt(res.data.pageSize);
      this.total = res.data.total;
      console.log(res.data);
    },
    //操作分页
    handleSizeChange(val) {
      console.log("--------一页多少条：", val);
      this.listQuery.pageSize = val;
      this.getList();
    },
    //操作分页
    handleCurrentChange(val) {
      console.log("--------当前页：", val);
      this.listQuery.currPage = val;
      this.getList();
    },
    //新增
    handleCreate() {
      this.addDialog = true;
    },
    // 监听对话框的关闭
    closeDialog() {
      this.is_edit = false;
      this.chapter = {};
    },
    //新增提交
    async handleCreateSubmit() {
      let res;
      if (!this.is_edit) {
        res = await this.$http.post("/insert_comic_chapter", {
          comic_chap_title: this.chapter.comic_chap_title,
          comic_id: this.comic_id,
        });
      } else {
        res = await this.$http.put("/update_comic_chapter", this.chapter);
      }

      this.$message({
        type: res.data.type,
        message: res.data.message,
      });
      this.getList();
      this.addDialog = false;
      //这里作演示用，正式新增 请提交到接口
      //   vm.list.push(vm.temp);
      //   console.log("新增后", vm.list);
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

  closeContentBox(){
    this.ContentDialog = false;
    this.contents = []
  },
    // 展示内容
    async showContent(row) {
      let comic_chap_id = row.comic_chap_id;
      this.comic_chap_id = comic_chap_id
      let res = await this.$http.get("/comic_contents", {
        params: { comic_chap_id },
      });
      res.data.forEach((item) => {
        this.contents.push({
          name: item.comic_con_id,
          url: item.comic_con_src,
        });
      });
      this.comic_chap_title = row.comic_chap_title;
      this.ContentDialog = true;
    },
    afterUpload(res){
      console.log(res);
      this.contents.push({
        name:res.filename,
        url:res.url
      })
    },  
    //上传内容
    async submitContent() {
      let res;
      res = await this.$http.put("/insert_comic_contents", {comic_chap_id:this.comic_chap_id,contents:this.contents});
      
      this.$message({
        type: res.data.type,
        message: res.data.message,
      });
      this.ContentDialog = false;
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
      this.contents = fileList
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      console.log(file);
      this.dialogVisibleIMG = true;
    }
  },
};
</script>
<style >
a:hover {
  color: rgb(237, 66, 89);
}
.upload-demo{
  text-align: center;
}
</style>
